﻿#@head() 
#@header()
<title>营销管理</title>
#@aside("marketing")
<style>
	.select2-container{width:150px;float:left;height:35px;margin-top:10px;border-radius:0px !important;}
	.select2-container--default .select2-selection--single{height:35px;}
</style>
<div class="content-wrapper">
	<section class="content">
		<div class="nav-tabs-custom">
            <ul class="nav nav-tabs">
			  <li class=""><a href="#(path)/admin/page/marketing-directional">优惠券发放</a></li>
             <!-- <li class=""><a href="#(path)/admin/page/marketing-activity">推广参数</a></li>-->
			  <li class="active"><a href="#(path)/admin/page/marketing-cashrelease">现金发放</a></li>
            </ul>
            <div class="tab-content">
				  <!--推广参数配置-->
			  <div class="tab-pane active" id="tab_2">
				<div class="box-body">
					<table id="example1" style="width:800px;" class="table table-striped">
						<tbody>
							<tr>
								<th align="right" style="width:100px;">发放方式：</th>
								<td>
									<select id="fangshi" class="form-control">
										<option value="1">向指定用户定向派发（无需用户参与领取，立即到账）</option>
										<option value="2">向指定批量用户定向派发（无需用户参与领取，立即到账）</option>
									 </select>
									 <div id="uploader" hidden style="margin-top:10px;" class="wu-example">
										<!--用来存放文件信息-->
										<div id="thelist" class="uploader-list"></div>
										<div class="btns">
											<div id="picker">选择文件</div>
										</div>
									 </div>
									
									<div  id="a2">
										<span style="float:left;margin:15px 10px 0 0;">向用户名</span>
										<select style="width:150px;height:35px;float:left;margin-top:10px;" id="userId" class="form-control"></select>
										<span style="float:left;margin:15px 10px 0 10px;">
											发放
										 </span>
										<input id="amount" style="width:120px;float:left;margin-top:10px;" type="text" class="form-control" placeholder="">
										<span id="danwei" style="float:left;margin:15px 0px 0 10px;">
											元
										 </span>
									</div> 
									<input hidden id="url"></input>
									 
								</td>
							</tr>
						</tbody>
					</table>
				</div>
				<div class="box-footer">
					<button id="submit" type="submit" style="margin-left:10px;" class="btn btn-info ">提交</button>
					
				  </div>
              </div>
            </div>
            <!-- /.tab-content -->
          </div>
		  
	</section>
</div> 
#@footer()
<script>
$(document).ready(function(){
	var src = "";
	var userid = "";
	var mode = "";
	
	$("#userId").select2({
		ajax: {
			type:'GET',
			url: "#(path)/user/list?token=#(session.token??)",
			dataType: 'json',
			delay: 1000,
			data: function (params) {
				return {
					mobile: params.term, // search term 请求参数
					page: params.page
				};
			},
			processResults: function (data, params) {
				var itemList = [];
				var arr = data.data.page.list
				for(item in arr){
					var user = arr[item];
					 
					itemList.push({id:user.id, text: user.real_name});
				}
				return {
					results: itemList
				 };
			},
			cache: true
		},
		placeholder:'请输入手机号搜索...',//默认文字提示
		language: "zh-CN",
		tags: false,//允许手动添加
		allowClear: true,//允许清空
		escapeMarkup: function (markup) { return markup; }, // 自定义格式化防止xss注入
		minimumInputLength: 1,//最少输入多少个字符后开始查询
		formatResult: function formatRepo(repo){return repo.text;}, // 函数用来渲染结果
		formatSelection: function formatRepoSelection(repo){return repo.text;} // 函数用于呈现当前的选择
	});
	
	$("#submit").click(function() {

		var bb = $("#fangshi").val();
		var amount = $("#amount").val();
		var couponID = $("#couponlist").val();
		 
		if(bb=="2"){
			mode = "multiple";
			file = $("#url").val();
			var url = "#(path)/couponLog/direct?token=#(session.token??)&mode="+mode+"&file="+file+"&userId="+userid+"&type=cash&amount="+amount; 
		}else if(bb=="1"){
			mode = "single";
			userid = $("#userId").val();
			var url = "#(path)/couponLog/direct?token=#(session.token??)&mode="+mode+"&userId="+userid+"&type=cash&amount="+amount;
		}
		layer.confirm('是否确认提交？', {btn : [ '确认', '取消' ]}, function() {
			layer.msg('发放中，请稍等...',function(){
				ajax(url,{},function(data){
				if(data.code=='success'){
					layer.msg(data.msg, {icon:1},function(){
					});
				}else if(data.code=='error'){
					layer.msg(data.msg, {icon : 2});
				}
			});
			});
		}, function() {
			layer.msg('已取消！', {icon : 1});
		});
	});
	$("#fangshi").change(function(){
		var aa = $("#fangshi").val();
		 
		if(aa=="2"){
			mode = "multiple";
			$("#uploader").show();
			$("#a2").hide();									
		} else if(aa=="1"){
			$("#a2").show();
			$("#uploader").hide();
		}	
		var uploader = WebUploader.create({
		// swf文件路径
		swf: '#(path)/assets/plugins/webuploader/Uploader.swf',

		auto: true,

		// 文件接收服务端。
		server: '#(path)/sys/uploadFile',

		// 选择文件的按钮。可选。
		// 内部根据当前运行是创建，可能是input元素，也可能是flash.
		pick: '#picker',

		// 不压缩image, 默认如果是jpeg，文件上传前会压缩一把再上传！
		resize: false
	});
	// 当有文件被添加进队列的时候
	uploader.on( 'fileQueued', function( file ) {
		$("#thelist").empty();
		$("#thelist").append( '<div id="' + file.id + '" class="item">' +
			'<h4 class="info">' + file.name + '</h4>' +
			'<p class="state">等待上传...</p>' +
		'</div>' );
	});
	// 文件上传过程中创建进度条实时显示。
	uploader.on( 'uploadProgress', function( file, percentage ) {
		var $li = $( '#'+file.id ),$percent = $li.find('.progress .progress-bar');

		// 避免重复创建
		if ( !$percent.length ) {
			$percent = $('<div class="progress progress-striped active">' +
			  '<div class="progress-bar" role="progressbar" style="width: 0%">' +
			  '</div>' +
			'</div>').appendTo( $li ).find('.progress-bar');
		}

		$li.find('p.state').text('上传中');
		$percent.css( 'width', percentage * 100 + '%' );
	});
	uploader.on( 'uploadSuccess', function(file,response) {
		 
		$('#'+file.id ).find('p.state').text('已上传');		
		src = "/attachment"+response['url'];
		 
		$("#url").val(src);
	});

	uploader.on( 'uploadError', function( file ) {
		$( '#'+file.id ).find('p.state').text('上传出错');
	});

	uploader.on( 'uploadComplete', function( file ) {
		$( '#'+file.id ).find('.progress').fadeOut();
	});	
	});
});
</script>
</body>
</html>

